//
//  TDAddressMessageGetC.m
//  TBRentClient
//
//  Created by tonbright on 2018/6/28.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TDAddressMessageGetC.h"

#import "TDYuDingCarSectionHeadView.h"
#import "TDAddressInfoGetCell.h"
#import <IQKeyboardManager.h>
#import "NetWorkRequest.h"
#import "BRStringPickerView.h"
@interface TDAddressMessageGetC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, assign) NSInteger selIndex;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)UIImage *getImage;//拍照获取到的照片-------

@property (nonatomic,copy)NSString *driveID;
@property (nonatomic,copy)NSString *driveName;
@property (nonatomic,copy)NSString *driveToTime;//到期时间


@property (nonatomic,copy)NSString *homeAddress;
@property (nonatomic,copy)NSString *estatetype;
@property (nonatomic,strong)NSArray *estatetypeTitleArray;
@end

@implementation TDAddressMessageGetC
- (void)viewDidLoad {
    [super viewDidLoad];
//    [self loadData];
    _estatetypeTitleArray = @[@"自有",@"租赁"];
    [self setupUI];
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
}

- (IBAction)navBtnClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setupUI {
    //设置导航条
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"TDAddressInfoGetCell" bundle:nil] forCellReuseIdentifier:@"TDAddressInfoGetCellId"];
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, -10, 0, -10)];
    }
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100;
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
}



#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //自定义cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self getIdentifier:indexPath] forIndexPath:indexPath];
    [self setupCell:cell indexPath:indexPath];
    return cell;
}



- (NSString *)getIdentifier:(NSIndexPath *)indexPath {
    
    NSString *iden = @"TDAddressInfoGetCellId";
    return iden;
}

- (void)setupCell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    TDAddressInfoGetCell *addressInfoGetCell = (TDAddressInfoGetCell *)cell;
    __weak TDAddressInfoGetCell *weakAddressInfoGetCell = addressInfoGetCell;
    __weak typeof(self) weakSelf = self;
    //选择点击事件---------------
    addressInfoGetCell.addressInfoGetCellSelTypeBlock = ^{
        [weakSelf.view endEditing:YES];
        [BRStringPickerView showStringPickerWithTitle:@"请选择房屋状态" dataSource:weakSelf.estatetypeTitleArray defaultSelValue:weakAddressInfoGetCell.tf_estatetypestatus.text resultBlock:^(id selectValue) {
            weakAddressInfoGetCell.tf_estatetypestatus.text = selectValue;
        }];
    };
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    TDYuDingCarSectionHeadView *headView = [TDYuDingCarSectionHeadView yuDingCarSectionHeadView];
    headView.lbe_title.text = @"驾驶人信息录入(3/3)";
    headView.frame = CGRectMake(0, 0, Width-40, 74);
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 74;
}


- (IBAction)finishBtnClick:(UIButton *)sender {
    [self.view endEditing:YES];
    TDAddressInfoGetCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    self.homeAddress = cell.tf_address.text;
    self.estatetype = cell.tf_estatetypestatus.text;
    
    if ([HelpObject isBlankString:_homeAddress]) {
        self.noticeText = @"请先填写住宅地址";
        return;
    }
    if ([HelpObject isBlankString:_estatetype]) {
        self.noticeText = @"请先填写房屋状态";
        return;
    }
    [self updateinfoMessage];
}



// MARK: - 加载数据
- (void)loadData {
    [self updateinfoMessage];
}

// MARK: - 上传个人信息-------
- (void)updateinfoMessage{
    [self progressHUDShowWithText:nil];
    __weak typeof(self) weakSelf = self;
    TDAddressInfoGetCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    for (int i = 0; i < self.estatetypeTitleArray.count; i++) {
        NSString *title = _estatetypeTitleArray[i];
        if ([title isEqualToString:cell.tf_estatetypestatus.text]) {
            self.estatetype = [NSString stringWithFormat:@"%d",i+1];
            break;
        }
    }
    
    NSDictionary *dic = @{
                          @"updateuserid":[HelpObject changeNull:[AppSetting shareAppSetting].memberid],
                          @"oldupdatetime":[HelpObject changeNull:[AppSetting shareAppSetting].updatetime],
                          @"homeaddress":self.homeAddress,
                          @"estatetype":self.estatetype,
                          };
    [NetWorkRequest postNetRequestData:UpdateMemberMessage parmeters:dic response:^(NSDictionary *response) {
        [weakSelf progressHUDHideImmediately];
        if (response) {
            [weakSelf progressHUDShowWithCompleteText:@"信息上传成功" isSucceed:YES];
            [weakSelf.navigationController popToRootViewControllerAnimated:YES];
        }
    } netError:^(NSError *error) {
        [weakSelf progressHUDShowWithCompleteText:@"信息上传失败" isSucceed:NO];
    }];
}


@end
